package org.zhq.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.zhq.Const;
import org.zhq.pojo.bo.ShopCartBO;
import org.zhq.service.ShopCartService;
import org.zhq.utils.CookieUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static org.zhq.utils.CookieUtils.getCookieValue;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    private final StringRedisTemplate redisTemplate;

    @Autowired
    public ShopCartServiceImpl(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void addToShopCart(ShopCartBO shopCartBO, String userId) {
        //前端用户在登录情况下,添加商品到购物车，会同时在后端同步购物车到redis缓存
        //判断购物车存在该商品 累加数量
        List<ShopCartBO> list;
        String shopCartStr = redisTemplate.opsForValue().get(Const.Redis.SHOP_CART + ":" + userId);
        if (StrUtil.isBlank(shopCartStr)) {
            list = CollUtil.newArrayList(shopCartBO);
        } else {
            list = JSONUtil.toList(JSONUtil.parseArray(shopCartStr), ShopCartBO.class);
            boolean isHaving = false;
            for (ShopCartBO item : list) {
                if (item.getSpecId().equals(shopCartBO.getSpecId())) {
                    item.setBuyCounts(item.getBuyCounts() + shopCartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                list.add(shopCartBO);
            }
        }
        redisTemplate.opsForValue().set(Const.Redis.SHOP_CART + ":" + userId, JSONUtil.toJsonStr(list));
    }

    @Override
    public void delFromShopCart(String itemSpecId, String userId) {
        //前端用户在登录情况下,从购物车中删除商品，会同时在后端同步购物车到redis缓存
        List<ShopCartBO> list;
        String shopCartStr = redisTemplate.opsForValue().get(Const.Redis.SHOP_CART + ":" + userId);
        if (StrUtil.isNotBlank(shopCartStr)) {
            list = JSONUtil.toList(JSONUtil.parseArray(shopCartStr), ShopCartBO.class);
            for (ShopCartBO item : list) {
                if (item.getSpecId().equals(itemSpecId)) {
                    list.remove(item);
                    break;
                }
            }
            redisTemplate.opsForValue().set(Const.Redis.SHOP_CART + ":" + userId, JSONUtil.toJsonStr(list));
        }
    }

    @Override
    public void syncShopCart(String userId, HttpServletRequest request, HttpServletResponse response) {
        String shopCartStrOnRedis = redisTemplate.opsForValue().get(Const.Redis.SHOP_CART + ":" + userId);
        String shopCartStrOnCookie = getCookieValue(request, Const.Redis.SHOP_CART);
        if (StrUtil.isNotBlank(shopCartStrOnRedis)) {
            List<ShopCartBO> shopCartBOListOnRedis = JSONUtil.toList(JSONUtil.parseArray(shopCartStrOnRedis), ShopCartBO.class);
            if (StrUtil.isNotBlank(shopCartStrOnCookie)) {
                //redis不为空 cookie不为空
                List<ShopCartBO> shopCartBOListOnCookie = JSONUtil.toList(JSONUtil.parseArray(shopCartStrOnCookie), ShopCartBO.class);
                List<ShopCartBO> toBeRemovedList = CollUtil.newArrayList();
                for (ShopCartBO item1 : shopCartBOListOnRedis) {
                    for (ShopCartBO item2 : shopCartBOListOnCookie) {
                        if (item1.getSpecId().equals(item2.getItemId())) {
                            item1.setBuyCounts(item2.getBuyCounts());
                            toBeRemovedList.add(item2);
                        }
                    }
                }
                shopCartBOListOnCookie.removeAll(toBeRemovedList);
                shopCartBOListOnRedis.addAll(shopCartBOListOnCookie);
                redisTemplate.opsForValue().set(Const.Redis.SHOP_CART + ":" + userId, JSONUtil.toJsonStr(shopCartBOListOnRedis));
                CookieUtils.setCookie(request, response, Const.Redis.SHOP_CART, JSONUtil.toJsonStr(shopCartBOListOnRedis), true);
            } else {
                //redis不为空 cookie为空 redis复制到cookie
                CookieUtils.setCookie(request, response, Const.Redis.SHOP_CART, JSONUtil.toJsonStr(shopCartBOListOnRedis), true);
            }
        } else {
            if (StrUtil.isNotBlank(shopCartStrOnCookie)) {
                //redis空 cookie不为空 cookie复制到redis中
                List<ShopCartBO> shopCartBOListOnCookie = JSONUtil.toList(JSONUtil.parseArray(shopCartStrOnCookie), ShopCartBO.class);
                redisTemplate.opsForValue().set(Const.Redis.SHOP_CART + ":" + userId, JSONUtil.toJsonStr(shopCartBOListOnCookie));
            }
        }
    }

    @Override
    public void cleanShopCart(String userId, HttpServletRequest request, HttpServletResponse response) {
//        redisTemplate.delete(Const.Redis.SHOP_CART + ":" + userId);
        CookieUtils.deleteCookie(request, response, Const.Redis.SHOP_CART);
    }
}
